const https = require('https');
const fs = require('fs');
const path = require('path');

const libs = [
    {
        url: 'https://html2canvas.hertzen.com/dist/html2canvas.min.js',
        filename: 'html2canvas.min.js'
    },
    {
        url: 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js',
        filename: 'jspdf.umd.min.js'
    }
];

const libDir = path.join(__dirname, 'lib');

// 确保lib目录存在
if (!fs.existsSync(libDir)) {
    fs.mkdirSync(libDir);
}

// 下载文件函数
function downloadFile(url, filename) {
    return new Promise((resolve, reject) => {
        const filePath = path.join(libDir, filename);
        const file = fs.createWriteStream(filePath);

        https.get(url, (response) => {
            response.pipe(file);
            file.on('finish', () => {
                file.close();
                console.log(`Downloaded ${filename}`);
                resolve();
            });
        }).on('error', (err) => {
            fs.unlink(filePath, () => {});
            reject(err);
        });
    });
}

// 下载所有库文件
async function downloadLibs() {
    for (const lib of libs) {
        try {
            await downloadFile(lib.url, lib.filename);
        } catch (error) {
            console.error(`Failed to download ${lib.filename}:`, error);
        }
    }
}

downloadLibs(); 